package cn.edu.nuc.android.oweibo.layouts;

import java.net.URL;

import weibo4android.Status;
import weibo4android.User;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.edu.nuc.android.oweibo.R;
import cn.edu.nuc.android.oweibo.tasks.LoadImageAsyncTask;
import cn.edu.nuc.android.oweibo.tasks.LoadImageAsyncTask.LoadImageAsyncTaskResponder;

public class StatusListItem extends RelativeLayout implements LoadImageAsyncTaskResponder {

	private TextView statusText;
	private TextView screenName;
	private ImageView avatarView;
	private AsyncTask<URL, Void, Drawable> latestLoadTask; // 加载用户头像

	public StatusListItem(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setStatus(Status status) {
		final User user = status.getUser();
		findViews();
		screenName.setText(user.getName());
		statusText.setText(status.getText());
		if (null != latestLoadTask) {
			latestLoadTask.cancel(true);
		}
		latestLoadTask = new LoadImageAsyncTask(this).execute(user.getProfileImageURL());
	}

	@Override
	public void imageLoading() {
		avatarView.setImageDrawable(null);
	}

	@Override
	public void imageLoadCancelled() {
		// do nothing
	}

	@Override
	public void imageLoaded(Drawable drawable) {
		avatarView.setImageDrawable(drawable);
	}

	private void findViews() {
		avatarView = (ImageView)findViewById(R.id.user_avatar);
		screenName = (TextView)findViewById(R.id.status_user_name_text);
		statusText = (TextView)findViewById(R.id.status_text);
	}

}
